<?php
/**
 * @Notes:【】
 * @User: zwc
 * @Date: 2022-04-30 16:17
 */

namespace App\Controller\Ws;

use App\Constant\TalkEventConstant;
use App\Constants\ChatEvent;
use App\Constants\Talk\MessageEvent;
use App\Event\Ocean\OceanNavigateTo;
use App\Event\TalkConnectEvent;
use App\Event\TalkEvent;
use App\Middleware\ws\UserSocketAuthMiddleware;
use App\Repository\WebSocket\UsersData;
use App\Service\WebSocket\SocketClientService;
use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\WebSocketServer\Context;
use Swoole\WebSocket\Frame;
use Swoole\Http\Request;

/**
 * @Middleware(UserSocketAuthMiddleware::class)
 */
class UserWsController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
{
    public function onMessage($server, Frame $frame): void
    {
        dump('222222');
        $result = json_decode($frame->data, true);

        // 判断是否为心跳检测
        if ($result['event'] == 'heartbeat') {
            $server->push($frame->fd, json_encode(['event' => "heartbeat", 'content' => "pong"]));
            return;
        }

        if (!MessageEvent::hasValues($result['event'])) {
            return;
        }
        dump($result['event']);

        if ($result['event'] == MessageEvent::NAVIGATE_TO || $result['event'] == MessageEvent::NAVIGATE_HOME) {
            event()->dispatch(new OceanNavigateTo($result['event'], $result['token'], $result['data']));
            return;
        }

        event()->dispatch(new TalkEvent($result['event'], $result['token'], $result['data']));
//        if (!isset(MessageEvent::EVENTS[$result['event']])) {
//            return;
//        }
//
//        // 回调处理
//        call_user_func_array([$this->receiveHandle, ReceiveHandleService::EVENTS[$result['event']]], [
//            $server, $frame, $result['data']
//        ]);

        //推送
//        $server->push($frame->fd, json_encode([
//            "event"   => "connect",
//            "content" => [
//                "ping_interval" => 20,
//                "ping_timeout"  => 20 * 3,
//            ],
//        ]));
//
//        $result = json_decode($frame->data, true);
//        dump($result);
//
////        dump($server);
//
    }

    public function onClose($server, int $fd, int $reactorId): void
    {
        var_dump('closed');

        $res = $this->SocketClientService->unbind($fd);
    }

    /**
     * @Inject()
     * @var SocketClientService
     */
    protected $SocketClientService;


    public function onOpen($server, Request $request): void
    {
        //推送
        $server->push($request->fd, json_encode([
            "event" => "connect",
            "content" => [
                "ping_interval" => 20,
                "ping_timeout" => 20 * 3,
            ],
        ]));

        //绑定
        $userData = UsersData::get();

        $this->SocketClientService->bind($request->fd, $userData);

        event()->dispatch(new TalkConnectEvent($userData->getUid(), $userData->getFrom()));
//        event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_LOGIN, [
//            'user_id' => $user_id,
//            'status' => 1,
//        ]);
    }
}